package com.ld.control.business.equipment.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ld.control.business.equipment.mapper.EquipmentMethodParamTmpMapper;
import com.ld.control.business.equipment.mapper.EquipmentMethodTmpMapper;
import com.ld.control.business.equipment.model.EquipmentMethodDebugParamModel;
import com.ld.control.business.equipment.model.EquipmentMethodParamTmpModel;
import com.ld.control.business.equipment.model.EquipmentMethodTmpModel;
import com.ld.control.business.equipment.service.EquipmentMethodDebugParamService;
import com.ld.control.business.equipment.service.EquipmentMethodParamTmpService;
import com.ld.control.business.equipment.vo.req.EquipmentMethodParamTmpReqVo;
import com.ld.control.business.equipment.vo.resp.EquipmentMethodParamTmpRespVo;
import com.ld.control.business.robot.service.RobotPointService;
import com.ld.control.business.robot.vo.resp.RobotPointRespVo;
import com.ld.control.common.equipment.param.OBRobotMethodParamConstant;
import com.ld.control.common.vo.KeyValueVo;
import com.ld.control.sysenum.NodeParamUseTypeEnum;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 设备方法参数表 服务实现类
 * </p>
 *
 * @author wangfeng
 * @since 2024-04-17
 */
@Service
public class EquipmentMethodParamTmpServiceImpl extends ServiceImpl<EquipmentMethodParamTmpMapper, EquipmentMethodParamTmpModel> implements EquipmentMethodParamTmpService {

    @Autowired
    private EquipmentMethodParamTmpMapper equipmentMethodParamTmpMapper;

    @Autowired
    private EquipmentMethodDebugParamService equipmentMethodDebugParamService;

    @Autowired
    private EquipmentMethodTmpMapper equipmentMethodTmpMapper;

    @Autowired
    private RobotPointService robotPointService;


    @Override
    public void add(EquipmentMethodParamTmpReqVo reqVo) {
        EquipmentMethodParamTmpModel equipmentMethodParamTmpModel = new EquipmentMethodParamTmpModel();
        BeanUtils.copyProperties(reqVo, equipmentMethodParamTmpModel);
        equipmentMethodParamTmpMapper.insert(equipmentMethodParamTmpModel);
    }

    @Override
    public void update(EquipmentMethodParamTmpReqVo reqVo) {
        EquipmentMethodParamTmpModel equipmentMethodParamTmpModel = new EquipmentMethodParamTmpModel();
        BeanUtils.copyProperties(reqVo, equipmentMethodParamTmpModel);
        equipmentMethodParamTmpMapper.updateById(equipmentMethodParamTmpModel);
    }

    @Override
    public void deleteById(Long id) {
        equipmentMethodParamTmpMapper.deleteById(id);
    }

    @Override
    public EquipmentMethodParamTmpRespVo getById(Long id) {
        EquipmentMethodParamTmpRespVo equipmentMethodParamTmpRespVo = new EquipmentMethodParamTmpRespVo();
        EquipmentMethodParamTmpModel equipmentMethodParamTmpModel = equipmentMethodParamTmpMapper.selectById(id);
        BeanUtils.copyProperties(equipmentMethodParamTmpModel, equipmentMethodParamTmpRespVo);
        return equipmentMethodParamTmpRespVo;
    }

    @Override
    public List<EquipmentMethodParamTmpRespVo> getList(Long equipmentMethodTmpId) {
        List<EquipmentMethodParamTmpRespVo> equipmentMethodParamTmpRespVoList = equipmentMethodParamTmpMapper.selectListByEquipmentMethodTmpId(equipmentMethodTmpId);
        // 特定方法需要生成paramSelect参数
        /*for (EquipmentMethodParamTmpRespVo data : equipmentMethodParamTmpRespVoList) {
            paramSelect(equipmentMethodTmpId, data);
        }*/
        return equipmentMethodParamTmpRespVoList;
    }

    @Override
    public List<EquipmentMethodParamTmpRespVo> getMethodDebugParamList(Long equipmentMethodTmpId) {
        List<EquipmentMethodParamTmpRespVo> respList = new ArrayList<>();
        List<EquipmentMethodParamTmpRespVo> equipmentMethodParamTmpRespVoList = equipmentMethodParamTmpMapper.selectListByEquipmentMethodTmpId(equipmentMethodTmpId);
        // 特定方法需要生成paramSelect参数
        EquipmentMethodParamTmpRespVo equipmentMethodParamTmpRespVo;
        for (EquipmentMethodParamTmpRespVo data : equipmentMethodParamTmpRespVoList) {
            if (NodeParamUseTypeEnum.RESP.getType() == data.getUseType()) {
                continue;
            }
            equipmentMethodParamTmpRespVo = new EquipmentMethodParamTmpRespVo();
            BeanUtils.copyProperties(data, equipmentMethodParamTmpRespVo);
            paramSelect(equipmentMethodTmpId, equipmentMethodParamTmpRespVo);
            respList.add(equipmentMethodParamTmpRespVo);
        }
        return respList;
        /*Map<String, EquipmentMethodDebugParamModel> equipmentMethodDebugParamModelMap = equipmentMethodDebugParamService.getMap(equipmentMethodTmpId);
        List<EquipmentMethodParamTmpRespVo> list = getList(equipmentMethodTmpId);
        for (EquipmentMethodParamTmpRespVo equipmentMethodParamTmpRespVo : list) {
            EquipmentMethodDebugParamModel equipmentMethodDebugParamModel = equipmentMethodDebugParamModelMap.get(equipmentMethodParamTmpRespVo.getName());
            if (equipmentMethodDebugParamModel != null) {
                equipmentMethodParamTmpRespVo.setValue(equipmentMethodDebugParamModel.getValue());
            }
            if (ObjectUtils.isEmpty(equipmentMethodParamTmpRespVo.getValue())) {
                equipmentMethodParamTmpRespVo.setValue(equipmentMethodParamTmpRespVo.getDefaultValue());
            }
        }
        return list;*/
    }

    private void paramSelect(Long equipmentMethodTmpId, EquipmentMethodParamTmpRespVo data) {
        List<KeyValueVo> keyValueVoList = new ArrayList<>();
        EquipmentMethodTmpModel equipmentMethodTmpModel = equipmentMethodTmpMapper.selectById(equipmentMethodTmpId);
        String methodTmpModelName = equipmentMethodTmpModel.getName();
        // 将机械臂点位信息写入到paramSelect
        if (methodTmpModelName.equals(OBRobotMethodParamConstant.Pick.internalMethodName)
                || methodTmpModelName.equals(OBRobotMethodParamConstant.Place.internalMethodName)
                || methodTmpModelName.equals(OBRobotMethodParamConstant.SingleAction.internalMethodName)
        ) {
            if (!data.getName().equals("robotPointName")) {
                return;
            }
            // 查询机械臂点位信息
            List<RobotPointRespVo> robotPointList = robotPointService.getList(null);
            KeyValueVo keyValueVo;
            for (RobotPointRespVo respVo : robotPointList) {
                keyValueVo = new KeyValueVo();
                keyValueVo.setKey(respVo.getName());
                keyValueVo.setValue(respVo.getName());
                keyValueVoList.add(keyValueVo);
            }
            if (!CollectionUtils.isEmpty(keyValueVoList)) {
                data.setValue(keyValueVoList.get(0).getValue());
                data.setParamSelect(keyValueVoList);
            }
        }
    }

}
